package com.suncnpap.statistics.controller;

import com.suncnpap.library.model.BorrowBookInfo;
import com.suncnpap.library.service.BorrowBookRecordService;
import com.suncnpap.library.service.BorrowBookService;
import com.suncnpap.library.service.LibraryService;
import com.suncnpap.system.model.School;
import com.suncnpap.system.service.SchoolService;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.app.util.common.HandlerResult;
import com.app.util.common.ObjUtil;
import com.app.util.common.ResponseUtil;
import com.app.util.database.mybatis.Page;
import com.suncnpap.base.controller.BaseController;

/** 
 * 说明：大数据看板Controller
 */
@Controller
@RequestMapping(value="/dataShow")
public class DataShowController extends BaseController {
	
	//private static final Logger log = Logger.getLogger(LibraryController.class);
	
	@Autowired
	LibraryService libraryService;
	
	@Autowired
	BorrowBookService borrowBookService;
	
	@Autowired
	BorrowBookRecordService borrowBookRecordService;
	
	@Autowired
	SchoolService schoolService;
	
	/**
	 * 大数据看板数据处理
	 * @param page
	 * @param response
	 */
	@RequestMapping(value="/index")
	public void index(HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		HandlerResult result = new HandlerResult();
		

		String strSchoolId = this.getParameter("strSchoolId", "");
		
		Page page = new Page();
		page.addQueryAnd("strSchoolId", strSchoolId);
		int allCount = libraryService.findCount(page);
		result.put("allCount", allCount);
		
		Page readerBorrowPage = this.setPage();
		readerBorrowPage.setUsePage(false);
		readerBorrowPage.removeQueryAnd("strSchoolId");
		readerBorrowPage.addQueryAnd("r.strSchoolId", strSchoolId);
		readerBorrowPage = borrowBookRecordService.readerBorrowGroupList(readerBorrowPage);
		List<BorrowBookInfo> list = readerBorrowPage.getDataList();
		result.put("borrowList", list);
		
		Map<String, List<BorrowBookInfo>> map = ObjUtil.listToMapByField(list, "strGradeClassName");
		List<Map<String, Object>> objList = new ArrayList<Map<String, Object>>();
        for (Map.Entry<String, List<BorrowBookInfo>> entry : map.entrySet()) {
            System.out.println(entry.getKey() + ": " + entry.getValue());
            Map<String, Object> objMap = new HashMap<String, Object>();
			objMap.put("title", entry.getKey());
			objMap.put("list", entry.getValue());
			objList.add(objMap);
        }
		
        result.put("classBorrowList", objList);
        
        School school = schoolService.findById(strSchoolId);
        if(school!=null){
        	result.put("strPicUrl", school.getStrPicUrl());
        }
        
        Page libraryBorrowPage = this.setPage();
        libraryBorrowPage.removeQueryAnd("strSchoolId");
        libraryBorrowPage.addQueryAnd("br.strSchoolId", strSchoolId);
        libraryBorrowPage = borrowBookRecordService.libraryBorrowList(libraryBorrowPage);
        List<BorrowBookInfo> libraryBorrowList = libraryBorrowPage.getDataList();
        result.put("libraryBorrowList", libraryBorrowList);
        
        Page countPage = this.setPage();
        countPage.removeQueryAnd("strSchoolId");
        countPage.addQueryAnd("br.strSchoolId", strSchoolId);
        BorrowBookInfo obj = borrowBookRecordService.libraryBorrowStatistics(countPage);
        
        DecimalFormat g1=new DecimalFormat("00000");
        if(obj!=null){
        	String strCount = obj.getStrCount();
        	int count = Integer.valueOf(strCount);
        	result.put("strCount", obj.getStrCount().length()==5?obj.getStrCount():(g1.format(count)+""));
        	result.put("intDayBorrowCount", obj.getIntDayBorrowCount()+"");
        	result.put("intDayReturnCount", obj.getIntDayReturnCount()+"");
        }
        
        Page hourBorrowStatisticsPage = this.setPage();
        hourBorrowStatisticsPage.addQueryAnd("strSchoolId", strSchoolId);
        hourBorrowStatisticsPage = borrowBookRecordService.hourBorrowStatistics(hourBorrowStatisticsPage);
        List<BorrowBookInfo> hourBorrowList = hourBorrowStatisticsPage.getDataList();
        result.put("hourBorrowList", hourBorrowList);
	        
		ResponseUtil.writeJson(result, response);

	} 
}
